;
(function($,win)
{
	/* @------------------------------------------------------------------------------------@ */
	$(document).ready(function()
	{
		var url = location.search;
		if (url.indexOf("?topictypeId=") != -1) {
			var reqPath = basePath+'topicInfoManager/queryTopicTypeById.action'+url;
			var sendBody = '{}';
			send(reqPath, "POST", "application/json", "json", sendBody, function(res)
			{
				if (res.result == 'success')
				{
					if (res.data != null)
					{
						debugger;
						var topicType = res.data;
						$("#uu_id").val(topicType.uuid);
						$("#type_name").val(topicType.topicTypeName);
						if(null!=topicType.listAdInfo)
						{
							var adInfo = '';
							$.each(topicType.listAdInfo, function(i,n)
									{
								adInfo += '<input type="checkbox" name="bindToId" value="'+n.uuid+'" checked="checked"/>'+n.businessName+' ';
									});
							$("#ad_info").append(adInfo);
						}
						if(null!=topicType.listUnAdInfo)
						{
							var unAdInfo = '';
							$.each(topicType.listUnAdInfo, function(i,n)
									{
								unAdInfo += '<input type="checkbox" name="bindToId" value="'+n.uuid+'" />'+n.businessName+' ';
									});
							$("#un_ad_info").append(unAdInfo);
						}
					} else
					{
						alert("没有查找到相关数据");
					}
				}
			});
		}else if (url.indexOf("?type=") != -1)
		{
			$('#parent_type').hide();
			$('#merchant_type').bind('click', function()
			{
				if($('#merchant_type').val()=='1')
				{
					$('#parent_type').hide();
					$('#merchant_img').show();
				}else if ($('#merchant_type').val()=='2')
				{
					$('#merchant_img').hide();
					$('#parent_type').show();
				}
			});
			queryParentType();
			queryAdList();
		}else
		{
			iniadinfoData(0,15);
		}
		$('#addnew').click(function()
		{
			window.location.href = jumpPath + 'view/technology/merchanttype/addmerchanttype.jsp?type=addmerchanttype';
		});

		// 查询
		$('#btn_search').bind('click', function()
		{
			iniadinfoData(0,15);
		});

		$('#tbn_save').bind('click', function()
		{
			savemerchanttype();
//			window.location = jumpPath + 'view/technology/merchanttype/merchanttypelist.jsp';
		});
		
		$('#tbn_update').bind('click', function()
		{
			updatetopictype();
		});
		
	});// the end

	/* @-----------------------------------------------------------------------------------------@ */
})(jQuery, window);

function edittopictype(topictypeId)
{
	window.location.href = jumpPath + 'view/technology/infotype/editinfotype.jsp?topictypeId='+topictypeId;
}

function del(id)
{

	if (confirm("确定要删除吗？"))
	{
		var url = "index.html";
		window.location.href = url;
	}
}

function iniadinfoData(pageNumber,pagesize)
{

	var reqPath = basePath+'topicInfoManager/queryTopicTypeByPage.action';
	var sendBody = '{"currentPage": 1,"pageSize": 15}';
	var $tab = $('#adinfoDataTables tbody').html("");

	
	var reqPath = basePath + 'topicInfoManager/queryTopicTypeByPage.action';
	var sendBody = '{"currentPage": '+pageNumber+',"pageSize": '+pagesize+'}';
	var $tab = $('#merchanttypeDataTables tbody').html("");

	send(reqPath, "POST", "application/json", "json", sendBody, function(res)
	{

		if (res.result == 'success')
		{

			if (res.data.data.length > 0)
			{
				var tpl_tr = $('#tpl_list').html(), rows = '';
				$.each(res.data.data, function(i,n)
				{
					var type = '';
					var operation = "<button onclick='edittopictype(\""+n.uuid+"\")' type='button' class='btn btn-primary'>绑定广告</button>";
					rows += tpl_tr.replace(/\$type/g, n.topicTypeName)
								  .replace(/\$operation/,operation);
				});
				$tab.append(rows);
				// page
				pagefunction(iniadinfoData,res.data.totalPages,pagesize);

			} else
			{
				$tab.append('<tr><td colspan="7">没有查找到相关数据。</td></tr>');
			}
		}
	});

}

function getLocalTime(nS) { 
	return new Date(parseInt(nS) * 1000).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " "); 
} 

/**
 * 
 * @METHODNAME: saveLoginadinfo
 * @METDHOD:
 * @Description: 添加登陆用户
 * @author Administrator
 * @date 2015年1月10日 下午5:37:52
 */
function savemerchanttype()
{
 	$('#frm_merchanttype').submit();
}


function updatetopictype()
{
	debugger;
	var bindToId = '';
	$("input:[name='bindToId']:checked").each(function() {
		bindToId +=$(this).val()+",";
	});
	var requsetPath = basePath + "topicInfoManager/updateTopicType.action";
	var requestData = $('#frm_merchanttype').serializeObject();
	requestData.bindToId = bindToId.substr(0,bindToId.length-1);
	requestData = JSON.stringify(requestData);
	console.log(requestData);
	send(requsetPath, "POST", "application/json", "json", requestData,
			function(resultData)
			{

				if (resultData.result == 'success')
				{
					// 跳转到列表
					window.location = jumpPath
							+ 'view/technology/infotype/infotypelist.jsp';
				} else
				{
					alert(resultData.errorMsg);
				}
			});
}

function queryParentType()
{
	var requsetPath = basePath + "merchantTypeManager/queryParentMerchantType.action";
	send(requsetPath, "POST", "application/json", "json", "{}",
			function(resultData)
			{

				if (resultData.result == 'success')
				{
					$.each(resultData.data, function(i,n)
					{
						$("#parent_id").append("<option value='"+n.uuid+"'>"+n.typeName+"</option>");
					});
				} else
				{
					alert(resultData.errorMsg);
				}
			});
}

function queryAdList()
{
	debugger;
	var requsetPath = basePath + "adManager/queryAdList.action";
	send(requsetPath, "POST", "application/json", "json", "{}",
			function(resultData)
			{

				if (resultData.result == 'success')
				{
					var adInfo = '';
					$.each(resultData.data, function(i,n)
					{
						adInfo += '<input type="checkbox" name="bindToId" value="'+n.uuid+'"/>'+n.businessName+' '
					});
					$("#ad_info").append(adInfo);
				} else
				{
					alert(resultData.errorMsg);
				}
			});
}
